<?php
/**
 * Created by PhpStorm.
 * User: s.utkin
 * Date: 08.09.14
 * Time: 13:56
 */

namespace Messenger\Messages;


use Messenger\Groups;
use Messenger\Messages;
use Users\Profile;
use Messenger\BaseAction;

class LastSeenTimeAction extends BaseAction {
	
	/**
	 * Указываем необходимые неявные параметры
	 * @return multitype:string
	 */
	static function pullParams() {
		return ['identity'];
	}

	/**
	 * Выводит время просмотра последнего отправленного сообщения
	 */
	protected function run()
	{
		$targetProfileId = $this->request->targetProfileId;
		$readTimestamp = $this->request->readTimestamp;

		$targetProfile = Profile::find($targetProfileId);

		if (!$targetProfile) {
			$this->response->noRender(true);
			$this->response->content = '';
			return;
		}

		if ((int)$readTimestamp < 1) {
			$myProfileId = $this->request->_identity;
			$groups = Groups::getList([$myProfileId, $targetProfileId]);

			if (count($groups) > 0) {
				$groupModel = $groups[0];
				$groupId = (string)$groupModel;
				$readTimestamp = (int)Messages::getLastReadTimestamp($groupId, $myProfileId);
			}
		}

		$this->response->userNickname = $targetProfile->nickname;
		$this->response->readTimestamp = $readTimestamp;
	}
}